﻿<!DOCTYPE html>
<html>
<head>
    <title>文档列表</title>
    $css()
    $iconcss()
    <style type="text/css">
        #dg .ui-table td {
            padding: 5px 10px;
            line-height: 125%;
        }

        #dg b {
        }

        .ui-datagrid-header {
            display: none;
        }

        dl dt {
            float: left;
        }
        .submitbar {
            padding: 10px 0 30px 60px;
        }
    </style>
</head>
<body>
    <div class="overhidden">
        <table cellpadding="0" cellspacing="0" width="100%" height="100%" style="margin:0;padding:0;">
            <tr>
                <td width="350px" class="control" valign="top">
                    <div class="top" style="padding-left:80px">

                        <input type="checkbox" id="ck_full_select" /><label for="ck_full_select">全选</label>
                        &nbsp;


                        <input type="checkbox" id="ck_child" name="ck_child" style="opacity: 1; filter: alpha(opacity=100); width: 10px; height: 10px;" />
                        <label for="ck_child" title="显示子栏目的文档">子栏目</label>

                        &nbsp;
                        &nbsp;
                        <input type="text" class="ui-box w80 small-search-box" id="search_key" />
                        <span class="ui-button w50 small-button" id="btn_search">
                            <span class=" button-inner">
                                <span class="button-txt">搜索</span>
                                <a href="javascript:;"></a>
                            </span>
                        </span>

                        <div class="list-menu" id="list-menu">
                            <span class="icon"></span>菜单
                            <div class="drop">
                                <a href="javascript:;" class="fn item" fn="create">$icon(create)新建文档</a>
                                <a href="javascript:;" class="fn item" fn="pubToSite">$icon(create)发布至其他站点</a>
                                <a href="javascript:;" class="fn item" onclick="menu.refresh()">$icon(refresh)刷新列表</a>
                                <a href="javascript:;" class="fn item last" fn="batchDel" style="color:#F00">$icon(delete)批量删除</a>
                            </div>
                        </div>


                    </div>

                    <div class="main" id="dg"></div>
                    <div class="ui-table-meta" id="list-pager">共-条</div>
                </td>
                <td valign="top">
                    <div id="pl" class="relative autoHeight autoScroll"></div>
                </td>
            </tr>
        </table>
    </div>

<script type="text/template" id="tpl">
        <div class="item" onclick="menu.edit('%id%')">
            <a href="javascript:;"><span>$icon(edit)</span>编辑</a>
        </div>
        <div class="item" onclick="menu.show('%id%')">
            <a href="javascript:;"><span>$icon(show)</span>预览</a>
        </div>
        <div class="item last" onclick="menu.del('%id%')">
            <a href="javascript:;"><span>$icon(delete)</span>删除</a>
        </div>
        <div class="line"></div>
        <div class="item" onclick="menu.moveSortNumber('%id%', 1)">
            <a href="javascript:;"><span>$icon(refresh)</span>向上移动</a>
        </div>

        <div class="item" onclick="menu.moveSortNumber('%id%',2)">
            <a href="javascript:;"><span>$icon(refresh)</span>向下移动</a>
        </div>
        <div class="item" onclick="menu.republish('%id%')">
            <a href="javascript:;"><span>$icon(refresh)</span>重新发布</a>
        </div>

        <div class="line"></div>
        <div class="item" onclick="menu.forword('%id%')">
            <a href="javascript:;"><span></span>转发文档</a>
        </div>
        <div class="item" onclick="menu.relatedContent('%id%')">
            <a href="javascript:;"><span>$icon(link)</span>文档关联</a>
        </div>
        <div class="line"></div>
        <div class="item" onclick="menu.comment('%id%')">
            <a href="javascript:;"><span>$icon(comment)</span>评论管理</a>
        </div>

    </script>
<script type="text/template" id="item_template" class="hidden">
        <div class="archive-item" valign="middle" style="overflow:hidden-x;width:100%">
            <span class="index">{index}</span>
            <span title="{title}">{title}</span>
            <br>
            <input class="ck" type="checkbox" style="border:none" value="{id}">
            <span class="time">{publish}</span>
            &nbsp;&nbsp;浏览:<em> {viewCount}</em> 次
        </div>
    </script>

$js()

<script type="text/javascript">

        jr.dom.fitHeight(document.body.getElementsByTagName('DIV')[0]);
        var itemTemplate = jr.$('item_template').innerHTML;
        var tplHtml = jr.$('tpl').innerHTML;
        var listMenu = jr.$('list-menu');
        jr.hover(listMenu);

        var pagerPram = {
            category_id: '${category_id}',
            publisher_id: '',
            page_size: 10,
            page_index: 1,
            lb_visible: -1,
            lb_special: -1,
            lb_system: -1,
            lb_page: -1,
            include_child:false
        };

        pagerPram.page_size = parseInt((document.documentElement.clientHeight - 40) / 50);
        pagerPram.include_child = jr.$('ck_child').checked;
        var _dg = jr.datagrid('dg', {
            url: '?module=archive&action=pagerJsonData',
            data: pagerPram,
            idField: 'id',
            columns: [
                {
                    field: 'title', title: '名称',  formatter: function (val, row, index) {
                        return jr.template(itemTemplate, {
                            id:row.id,
                            index: (index + 1),
                            title: val.length > 25 ? val.substring(0, 25) + '...' : val,
                            publish: row.createDate.replace('T', ' '),
                            viewCount:row.view_count
                        });
                    }
                }
            ],
            loaded: function (data) {
                var tables = document.getElementsByTagName('TABLE');
                var tds = tables[2].getElementsByTagName('TD');
                var allowMultSelect = false;
                jr.table.dynamic(tables[2], allowMultSelect, function (trs) {
                    var id = trs[0].getAttribute('data-indent');
                    jr.load2('pl', 'archive', 'view', 'archive.id=' + id);
                });
                jr.each(tds, function (i, e) {
                    jr.contextmenu.bind(e, null, function (menu) {
                        menu.innerHTML = jr.template(tplHtml, { id: e.parentNode.getAttribute('data-indent') });
                    });
                });
                jr.each(jr.dom.getsByClass(document.body, 'ui-datagrid-view'), function (i, e) {
                    var tbs = e.getElementsByTagName('TABLE');
                    if (tbs.length != 0) {
                        tbs[0].className = 'ui-table ui-table-list';
                    }
                });


                //加载分页
                jr.$('list-pager').innerHTML = data.pager;

                if (jr.$('pl').innerHTML == '' && data.rows.length != 0) {
                    jr.load2('pl', 'archive', 'view', 'archive.id=' + data.rows[0].id);
                }

                // 重新绑定事件
                reBindCkEvent();
            }
        });

         _dg.resize();

        function toPage(i) {
            pagerPram.page_index = i;
            _dg.reload(pagerPram);
        }

        jr.$('ck_child').onchange = function() {
            pagerPram.include_child = this.checked;
            pagerPram.page_index = 1;
            _dg.reload(pagerPram);
        };

        jr.$('btn_search').onclick = function() {
            pagerPram.keyword = jr.$('search_key').value;
            pagerPram.page_index = 1;
            _dg.reload(pagerPram);
        };

        //创建
        Fn.create = function () {
            jr.load2('pl', 'archive', 'create', 'category.id=${category_id}', initEditor);
        };

    

        // 全选
        var ckList = [];
        Fn.pubToSite = function () {
            if (ckList.length === 0) {
                alert('请先选择文档');
            } else {
                jr.load2('pl', 'assistant', 'pub_archive', 'source_site=${site_id}&id_array='+ckList.join(','));
            }
        };

        Fn.batchDel = function() {
            if (ckList.length === 0) {
                alert('请先选择文档');
            } else if (confirm('确定删除吗?删除后将不可恢复!')) {
                jr.xhr.jsonPost('?', 'module=archive&action=batch_delete&id_array=' + ckList.join(','),
                    function(json) {
                        if (!json.result) {
                            showErr('部分文档删除失败！原因：'+json.message);
                        }
                        _dg.reload();
                    }
                );
            }
        };

        var fullCk = jr.$('ck_full_select');
        fullCk.checked = false;
        function reBindCkEvent() {
            ckList = [];
            var cks = jr.dom.getsByClass(document.body, 'ck');
            jr.each(cks, function(i, e) {
                e.onchange = function (event) {
                    ckList = getCkList();
                    if (window.pubList) window.pubList(ckList);
                    jr.event.stopBubble(event);
                    return false;
                };
            });
            if (fullCk.checked) {
                fullCk.checked = false;
                fullCk.onchange();
            }
        }

        function getCkList() {
            var list = [];
            var cks = jr.dom.getsByClass(document.body, 'ck');
            jr.each(cks, function (i, e) {
                if (e.checked) {
                    list.push(e.value);
                }
            });
            return list;
        }

        fullCk.onchange = function () {
            var isCheckd = this.checked;
            var cks = jr.dom.getsByClass(document.body, 'ck');
            jr.each(cks, function (i, e) {
                if (e.checked !== isCheckd) {
                    e.checked = isCheckd;
                }
            });
            ckList = getCkList();
            if (window.pubList)window.pubList(ckList);
        };



        window.menu = {
            refresh: function () {
                _dg.reload();
            },
            add: Fn.create,
            show:function(id) {
                window.open('?module=archive&action=view_frame&archive_id=' + id);
            },
            //编辑文档
            edit: function (id) {
                jr.load2('pl', 'archive', 'update', 'archive.id=' + id, initEditor);
            },

            //删除文档
            del: function (id) {
                if (confirm('确定删除吗?删除后将不可恢复!')) {
                    jr.xhr.jsonPost('?', 'module=archive&action=delete&archive.id=' + id,
                    function (json) {
                        if (json.result) {
                            jr.$('pl').innerHTML = '';
                            _dg.reload();
                        }
                        else {
                            showErr(json.message);
                        }
                    }
                    );
                }
            },
            forword:function(id){
                var d = jr.dialog.create2("文档转发", true, true);
                d.open("?module=archive&action=forword&archive.id=" + id,600,400,'auto');
            },
            //评论管理
            comment: function (id) {
                jr.load2('pl', 'archive', 'comment', 'desc=true&archive.id=' + id);
            },
            moveSortNumber: function (id,val) {
                jr.xhr.jsonPost('?', 'module=archive&action=moveSortNumber&archive.id=' + id+'&direction='+val,
                  function (json) {
                      if (json.result) {
                          _dg.reload();
                      } else {
                          showErr(json.message);
                      }
                  });
            },
            //重新发布
            republish: function (id) {
                jr.xhr.jsonPost('?', 'module=archive&action=republish&archive.id=' + id,
                  function (json) {
                      if (json.result) {
                          showMsg2('已经刷新最后更新时间！');
                          _dg.reload();
                      } else {
                          showErr(json.message);
                      }
                  });
            },
            relatedContent: function (id) {
                jr.$('pl').innerHTML = '<iframe frameborder="0" scrolling="no" width="100%" height="100%" ' +
                    'src="?module=link&action=related_link&content_id=' + id + '&content_type=archive"></ifarme>';
            }
        };
    </script>

    <!-- 编辑器 -->
    <script type="text/javascript" src="/public/assets/editor/kindeditor-all-min.js"></script>
    <script type="text/javascript" src="/public/assets/editor/plugins/code/prettify.js"></script>
    <script type="text/javascript">
        var editor = null;
        function initEditor() {
            editor = KindEditor.create('#editor', {
                cssPath: '/public/assets/editor/plugins/code/prettify.css',
                uploadJson: '/public/assets/editor/handler/upload_json.ashx',
                fileManagerJson: '/public/assets/editor/handler/file_manager_json.ashx',
                allowFileManager: true,
                filterMode: false
            });
        }
        function appendEditorStyle(){
            var ifrs = document.getElementsByClassName('ke-edit-iframe');
            var css = document.createElement('STYLE');
            css.innerHTML='.ke-content p{text-indent:0em;}';

            jr.each(ifrs,function(i,e){
                e.contentWindow.document.body.appendChild(css);
            });
        }
    </script>
</body>
</html>
